<template>
  <div class="widget_settings_pro">
    <ele-form-section
        v-model="formData"
        :request-fn="handleSubmit"
        :sections="sections"
        @request-success="handleSuccess"
    >
      <template v-slot:blocks="{ desc, data, field, formData }">
        <el-collapse v-model="activeNames">
          <draggable v-model="formData.templateBlockList" chosenClass="active" animation="500">
            <template v-for="(item, index) in formData.templateBlockList">
              <el-collapse-item title="图片" :name="index" class="game-select editForm" :key="index">
                <template slot="title">
                  <div class="custom-title">
                    <svg-icon icon-class="grid" class="icon"></svg-icon>
                    <img :src="item.settings.image" style="height: 30px" v-if="item.settings.image">
                  </div>
                  <span>图文</span>
                </template>
                <div class="form-item">
                  <label>图片是否展示</label>
                  <el-switch v-model="item.settings.enable_image"/>
                </div>
                <div class="form-item">
                  <label>PC端图片</label>
                  <ele-form-upload-plus-file v-model="item.settings.image" title="图片"  />
                  <p class="desc">建议尺寸：宽度800px,高度可自适应</p>
                </div>
                <div class="form-item">
                  <label>链接</label>
                  <ele-form-custom-select v-model="item.settings.url"/>
                </div>
                <div class="form-item">
                  <label>标题</label>
                  <el-input type="text" v-model="item.settings.title"></el-input>
                </div>
                <div class="form-item">
                  <label>正文</label>
                  <ele-form-quill-edit v-model="item.settings.text"></ele-form-quill-edit>
                </div>
                <div class="form-item">
                  <el-button v-if="index !== 0" class="addBtn" @click="handleRemove(index)"><i
                      class="el-icon-remove"></i>删除内容
                  </el-button>
                </div>
              </el-collapse-item>
            </template>
          </draggable>
        </el-collapse>
        <el-button type="primary" size="small" class="addBtn" @click="addPic"><i class="el-icon-plus"></i>添加图片
        </el-button>
      </template>
      <template v-slot:form-btn="{ btns }">
        <div class="rest-btn" @click="reset">
          <svg-icon icon-class="reset-settings"></svg-icon>
          重置配置
        </div>
      </template>
    </ele-form-section>
  </div>
</template>

<script>
import draggable from 'vuedraggable';
import {debounce, deepClone} from '@/utils/utils';

export default {
    name: 'featureColumnsForm',
    components: {draggable},
    props: {
        value: {
            type: Object,
            default: () => ({})
        }
    },
    watch: {
        value: {
            handler (newVal, oldVal) {
                this.formData = JSON.parse(newVal.styleJson);
            },
            immediate: false,
            deep: true
        },
        formData: {
            handler (newVal, oldVal) {
                this.pageCount++;
                if (this.pageCount !== 1) {
                    this.setData();
                }
            },
            immediate: false,
            deep: true
        }
    },
    data () {
        return {
            pageCount: 1,
            formData: this.value ? JSON.parse(this.value.styleJson) : {},
            activeNames: [],
            sections: [
                {
                    title: '设置',
                    formDesc: {
                        title: {
                            type: 'input',
                            label: '标题'
                        },
                        'align_text': {
                            type: 'select',
                            label: '文字对齐方式',
                            options: [
                                {text: '居左', value: 'left'},
                                {text: '居中', value: 'center'}
                            ]
                        }
                    }
                },
                {
                    title: '内容',
                    formDesc: {
                        'blocks': {}
                    }
                }
            ],
            predefineColors: [
                '#ff4500',
                '#ff8c00',
                '#ffd700',
                '#90ee90',
                '#00ced1',
                '#1e90ff',
                '#c71585',
                'rgba(255, 69, 0, 0.68)',
                'rgb(255, 120, 0)',
                'hsv(51, 100, 98)',
                'hsva(120, 40, 94, 0.5)',
                'hsl(181, 100%, 37%)',
                'hsla(209, 100%, 56%, 0.73)'
            ]
        };
    },
    methods: {
        addPic () {
            const params = deepClone(this.formData.defaultBlockList[0]);
            this.formData.templateBlockList.push(params);
        },
        handleResponse (response, file, fileList) {
            return response;
        },
        handleSubmit (data) {
            console.log(data);
            return Promise.resolve();
        },
        handleSuccess () {
            this.$message.success('创建成功');
        },
        handleRemove (index, record) {
            this.formData.templateBlockList.splice(index, 1);
        },
        reset () {
            this.pageCount = 0;
            this.$emit('resetData', this.value);
        },
        setData: debounce(function () {
            let data = deepClone(this.value);
            data.styleJson = JSON.stringify(this.formData);
            this.$store.commit('SET_PROJECT_DATA', data);
        }, 600)
    }
};
</script>

<style scoped lang="scss">
.form-item-inline {
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 10px;
  padding-right: 10px;
  color: #606266;
  height: 32px;
  margin-bottom: 10px;
}

.editForm {
  margin-bottom: 10px;

  .form-item {
    margin-top: 10px;

    label {
      display: block;
      height: 32px;
      line-height: 32px;
    }

    .desc {
      color: #666666;
      font-size: 14px;
      line-height: 20px;
      font-weight: 400;
      text-transform: initial;
      word-wrap: break-word;
      word-break: break-word;
      margin-top: 10px;
      margin-bottom: 10px;
      display: block;
      clear: both;
    }
  }

  ::v-deep .el-collapse-item__content {
    border: 1px solid #d8deec;
    border-top: 0;
    padding-left: 10px;
  }
}

.addBtn {
  width: 100%;
}
.custom-title{
  display: flex;
  align-items: center;
  .icon, img{
    margin-right: 6px;
  }
}
</style>
